/*
 * Copyright 2025 arisgo@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.arisgo.cloud.common.utils;


import com.arisgo.cloud.common.exception.CloudException;

import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Map;

/**
 * 断言工具
 *
 * @author Arisgo
 * @since 2024/01/20
 */
public class AssertUtil {

    public static void isTrue(boolean expr, String msg) {
        if (!expr) {
            runtimeException(CloudException.class, msg);
        }
    }

    public static void isTrue(boolean expr, Class<? extends RuntimeException> clazz, Object... msg) {
        if (!expr) {
            runtimeException(clazz, msg);
        }
    }

    public static void isFalse(boolean expr, String msg) {
        if (expr) {
            runtimeException(CloudException.class, msg);
        }
    }

    public static void isFalse(boolean expr, Class<? extends RuntimeException> clazz, Object... msg) {
        if (expr) {
            runtimeException(clazz, msg);
        }
    }

    public static void notEmpty(Object[] array, String msg) {
        if (null == array || array.length == 0) {
            runtimeException(CloudException.class, msg);
        }
    }

    public static void notEmpty(Object[] array, Class<? extends RuntimeException> clazz, Object... msg) {
        if (null == array || array.length == 0) {
            runtimeException(clazz, msg);
        }
    }

    public static void notEmpty(Collection<?> collections, String msg) {
        if (null == collections || collections.isEmpty()) {
            runtimeException(CloudException.class, msg);
        }
    }

    public static void notEmpty(Collection<?> collections, Class<? extends RuntimeException> clazz, Object... msg) {
        if (null == collections || collections.isEmpty()) {
            runtimeException(clazz, msg);
        }
    }

    public static void notEmpty(Map<?, ?> map, String msg) {
        if (null == map || map.isEmpty()) {
            runtimeException(CloudException.class, msg);
        }
    }

    public static void notEmpty(Map<?, ?> map, Class<? extends RuntimeException> clazz, Object... msg) {
        if (null == map || map.isEmpty()) {
            runtimeException(clazz, msg);
        }
    }

    public static void hasLength(String value, String msg) {
        if (null == value || value.trim().isEmpty()) {
            runtimeException(CloudException.class, msg);
        }
    }

    public static void hasLength(String value, Class<? extends RuntimeException> clazz, Object... msg) {
        if (null == value || value.trim().isEmpty()) {
            runtimeException(clazz, msg);
        }
    }

    public static void notNull(Object object, String msg) {
        if (null == object) {
            runtimeException(CloudException.class, msg);
        }
    }

    public static void notNull(Object object, Class<? extends RuntimeException> clazz, Object... msg) {
        if (null == object) {
            runtimeException(clazz, msg);
        }
    }

    public static void isNull(Object object, String msg) {
        if (null != object) {
            runtimeException(CloudException.class, msg);
        }
    }

    public static void isNull(Object object, Class<? extends RuntimeException> clazz, Object... msg) {
        if (null != object) {
            runtimeException(clazz, msg);
        }
    }

    private static void runtimeException(Class<? extends RuntimeException> clazz, Object... msg) {
        if (null == clazz) {
            if (ArgsUtil.isEmpty(msg)) {
                throw new RuntimeException();
            } else {
                throw new RuntimeException(msg[0].toString());
            }
        }

        try {
            if (ArgsUtil.isEmpty(msg)) {
                throw clazz.getConstructor().newInstance();
            } else {
                Class<?>[] classes = new Class[msg.length];
                for (int i = 0; i < msg.length; i++) {
                    classes[i] = msg[i].getClass();
                }
                throw clazz.getConstructor(classes).newInstance(msg);
            }
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException |
                 NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
    }
}
